package com.example.yuecvideo.activity;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.icu.text.CollationKey;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.yuecvideo.R;
import com.example.yuecvideo.fragment.HomeFragment;
import com.example.yuecvideo.fragment.UserFragment;

public class HomePageActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup radioGroup;
    private Fragment[] fragments;
    private FrameLayout mLayout;

    private String userName;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        initView();
        initFragment();
        setListener();
    }


    private void setListener() {
        radioGroup.setOnCheckedChangeListener(this);
    }

    private void initFragment() {
        Bundle bundle = getIntent().getExtras();
        fragments = new Fragment[2];
        fragments[0] = new HomeFragment();
        fragments[1] = new UserFragment();
        fragments[1].setArguments(getIntent().getExtras());
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.frame_layout, fragments[0]);
        transaction.commit();
        radioGroup.check(R.id.radio_home);
    }

    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        mLayout = (FrameLayout) findViewById(R.id.frame_layout);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        switch (checkedId) {
            case R.id.radio_home:
                transaction.replace(R.id.frame_layout, fragments[0]);
                break;
            case R.id.radio_user:
                transaction.replace(R.id.frame_layout, fragments[1]);
                break;
        }
        transaction.commit();
    }
}